import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */ '../components/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    redirect: '/welcome',
    component: () => import(/* webpackChunkName: "Home" */ '../components/Home.vue'),
    children: [
      {
        path: '/welcome',
        component: () => import(/* webpackChunkName: "Home" */ '../components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import(/* webpackChunkName: "Home" */ '../components/user/User.vue')
      },
      {
        path: '/rights',
        component: () => import(/* webpackChunkName: "Home" */ '../components/power/Rights.vue')
      },
      {
        path: '/roles',
        component: () => import(/* webpackChunkName: "Home" */ '../components/power/Roles.vue')
      }
    ]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  /**
   * to---要访问的路径
   * from---从哪个路径跳转过来的
   * next 是一个函数，执行就代表放行
   * next("/login")
   */
  if (to.path === '/login') {
    return next()
  }
  const tokenstr = sessionStorage.getItem('token')
  if (!tokenstr) {
    return next('/login')
  }
  next()
})
export default router
